/**
* @project admin-go-api
* @name sysMenu
* @descriptions 菜单数据层
* @author 寒锋
* @date 2023-12-19 14:36:40
 */

package dao

import (
	"admin-go-api/api/entity"
	"admin-go-api/common/util"
	"admin-go-api/constants"
	. "admin-go-api/pkg/db"
	"fmt"
	"time"
)

// GetSysMenuByName 根据菜单名称获取菜单信息
func GetSysMenuByName(menuName string, menuId int) (sysMenu entity.SysMenu) {
	model := Db.Where("menu_name = ?", menuName)
	if menuId != 0 {
		model = model.Where("menu_id <> ?", menuId)
	}
	model.First(&sysMenu)
	return sysMenu
}

// GetSysMenuById 根据id获取菜单信息
func GetSysMenuById(menuId int) (sysMenu entity.SysMenu) {
	Db.First(&sysMenu, menuId)
	return sysMenu
}

// QuerySysMenuVoList 获取可选菜单列表
func QuerySysMenuVoList() (sysMenu []entity.SysMenu) {
	Db.Table("sys_menu").Select("menu_id,parent_id,menu_name").Scan(&sysMenu)
	return sysMenu
}

// CreateSysMenu 添加菜单
func CreateSysMenu(sysMenu entity.SysMenu) bool {
	data := entity.SysMenu{
		ParentId:   0,
		MenuName:   sysMenu.MenuName,
		MenuType:   sysMenu.MenuType,
		MenuStatus: sysMenu.MenuStatus,
		Sort:       sysMenu.Sort,
		CreateTime: util.HTime{Time: time.Now()},
	}
	if sysMenu.MenuType == entity.TypeCatalog || sysMenu.MenuType == entity.TypeMenu {
		data.Url = sysMenu.Url
		data.Icon = sysMenu.Icon
	}
	if sysMenu.MenuType == entity.TypeMenu || sysMenu.MenuType == entity.TypeBtn {
		data.ParentId = sysMenu.ParentId
		data.Value = sysMenu.Value
	}
	fmt.Println(data)
	result := Db.Create(&data)
	if result.Error != nil {
		return false
	}
	return true
}

// UpdateSysMenu 更新菜单
func UpdateSysMenu(sysMenu entity.SysMenu) bool {
	data := entity.SysMenu{
		ParentId:   0,
		MenuName:   sysMenu.MenuName,
		MenuType:   sysMenu.MenuType,
		MenuStatus: sysMenu.MenuStatus,
		Sort:       sysMenu.Sort,
	}
	if sysMenu.MenuType == entity.TypeCatalog || sysMenu.MenuType == entity.TypeMenu {
		data.Url = sysMenu.Url
		data.Icon = sysMenu.Icon
	}
	if sysMenu.MenuType == entity.TypeMenu || sysMenu.MenuType == entity.TypeBtn {
		data.ParentId = sysMenu.ParentId
		data.Value = sysMenu.Value
	}
	result := Db.Where("menu_id = ?", sysMenu.MenuId).Updates(&data)
	return result.Error == nil
}

// DeleteSysMenu 删除菜单
func DeleteSysMenu(dto entity.SysMenuIdDto) error {
	// 启动事务
	tx := Db.Begin()
	if err := tx.Error; err != nil {
		return err
	}
	if err := tx.Where("parent_id = ?", dto.MenuId).Delete(&entity.SysMenu{}).Error; err != nil {
		tx.Rollback()
		return err
	}
	if err := tx.Delete(&entity.SysMenu{}, dto.MenuId).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}

// GetSysMenuList 菜单列表
func GetSysMenuList(menuName string, menuStatus int) (sysMenu []entity.SysMenu) {
	model := Db.Table("sys_menu").Order("sort")
	if menuName != "" {
		model = model.Where("menu_name = ?", menuName)
	}
	if menuStatus != 0 {
		model = model.Where("menu_status = ?", menuStatus)
	}
	model.Find(&sysMenu)
	return sysMenu
}

// QueryMenuVoList 当前登录用户左侧菜单级列表
func QueryMenuVoList(AdminId uint, MenuId uint) (menuSvo []entity.MenuSvo) {
	Db.Table("sys_menu sm").
		Select("sm.menu_name,sm.icon,sm.url").
		Joins("LEFT JOIN sys_role_menu srm ON sm.menu_id = srm.menu_id").
		Joins("LEFT JOIN sys_role sr ON sr.role_id = srm.role_id").
		Joins("LEFT JOIN sys_admin_role sar ON sar.role_id = sr.role_id").
		Joins("LEFT JOIN sys_admin sa ON sa.admin_id = sar.admin_id").
		Where("sr.status = ?", constants.StatusNormal).
		Where("sm.menu_status = ?", entity.MenuStatusNormal).
		Where("sm.menu_type = ?", entity.TypeMenu).
		Where("sm.parent_id = ?", MenuId).
		Where("sa.admin_id = ?", AdminId).
		Order("sm.sort").
		Scan(&menuSvo)
	return menuSvo
}

// QueryLeftMenuList 当前登录用户左侧菜单
func QueryLeftMenuList(AdminId uint) (leftMenuList []entity.LeftMenuVo) {
	Db.Table("sys_menu sm").
		Select("sm.menu_id, sm.menu_name, sm.url, sm.icon").
		Joins("LEFT JOIN sys_role_menu srm ON sm.menu_id = srm.menu_id").
		Joins("LEFT JOIN sys_role sr ON sr.role_id = srm.role_id").
		Joins("LEFT JOIN sys_admin_role sar ON sar.role_id = sr.role_id").
		Joins("LEFT JOIN sys_admin sa ON sa.admin_id = sar.admin_id").
		Where("sr.status = ?", constants.StatusNormal).
		Where("sm.menu_status = ?", entity.MenuStatusNormal).
		Where("sm.menu_type = ?", entity.TypeCatalog).
		Where("sa.admin_id = ?", AdminId).
		Order("sm.sort").
		Scan(&leftMenuList)
	return leftMenuList
}

// QueryPermissionList 当前登录用户的权限列表
func QueryPermissionList(AdminId uint) (valueVo []entity.ValueVo) {
	Db.Table("sys_menu sm").
		Select("sm.value").
		Joins("LEFT JOIN sys_role_menu srm ON sm.menu_id = srm.menu_id").
		Joins("LEFT JOIN sys_role sr ON sr.role_id = srm.role_id").
		Joins("LEFT JOIN sys_admin_role sar ON sar.role_id = sr.role_id").
		Joins("LEFT JOIN sys_admin sa ON sa.admin_id = sar.admin_id").
		Where("sr.status = ?", constants.StatusNormal).
		Where("sm.menu_status = ?", entity.MenuStatusNormal).
		Not("sm.menu_type = ?", entity.TypeCatalog).
		Where("sa.admin_id = ?", AdminId).
		Scan(&valueVo)
	return valueVo
}
